<?php

/*
 * ActiviteController.php
 */

namespace RHX\Controller;

use RHX\Model\Object\CodeActivite;

class ActiviteController extends RHXController {

    /**
     * Page d'accueil des Codes d'activités
     *
     * @return array
     */
    public function indexAction() {
	return array();
    }

    public function loadIndexAction() {
	// Changement du Layout
	$this->layout('layout/ajax');

	// Paramèters
	$page = $this->params()
		->fromRoute('id', 1);

	// Tables
	$codeTable = $this->getService('Table\CodeActiviteTable');

	// Liste des codes activités
	$codeList = $codeTable->getCodes('id ASC', $page, 30);

	return array(
	    'codeList' => $codeList,
	);
    }

    /**
     * Ajouter un nouveau Code
     * 
     * @return array
     */
    public function addNewAction() {
	// Changement de layout
	$this->layout('layout/ajax');

	// Données en POST
	$post = $this->getRequest()
		->getPost();

	// Tables
	$codeTable = $this->getService('Table\CodeActiviteTable');

	// Ajout
	$code		 = new CodeActivite($post);
	$code->id	 = $codeTable->save($code);

	return array(
	    'code' => $code,
	);
    }

    /**
     * Recharger la saisie des Nouveaux
     *
     * @return array
     */
    public function reloadNewAction() {
	// Changer de Template
	$this->layout('layout/ajax');

	return array();
    }

    public function saveAction() {
	// Changement de Layout
	$this->layout('layout/ajax');

	// Données en POST
	$post = $this->getRequest()
		->getPost();

	// Table
	$codeTable = $this->getService('Table\CodeActiviteTable');

	// Sauvegarde
	$code = new CodeActivite($post);
	$codeTable->save($code);

	return array(
	    'code' => $code,
	);
    }

    public function eraseAction() {
	// Changement de layout
	$this->layout('layout/ajax');

	// Paramètres
	$id = (int) $this->params()
			->fromRoute('id', 0);

	// Table
	$codeTable = $this->getService('Table\CodeActiviteTable');

	// Effacer
	$codeTable->erase($id);

	return array('id' => $id);
    }

    /**
     * Sauvegarde des données en édition
     * 
     * @return \Zend\View\Model\JsonModel
     */
    public function saveStringDataAction() {
	// Changement de layout
	$this->layout('layout/ajax');
	// Adapter de base de données
	$adapter = $this->getAdapter();
	// POST
	$post	 = $this->getRequest()
		->getPost();
	$pk	 = $post->pk;
	$name	 = $post->name;
	$value	 = addslashes($post->value);
	// Requête SQL
	$sql	 = "UPDATE rh_code_activite SET $name = '$value' WHERE id = $pk";
	$result	 = $adapter->query($sql)
		->execute();
	$data	 = array(
	    'done'	 => false,
	    'error'	 => null,
	);
	if ($result) {
	    $data['done'] = true;
	}
	else {
	    $data['done']	 = false;
	    $data['error']	 = mysql_error();
	}

	return new \Zend\View\Model\JsonModel($data);
    }

}

?>
